<?php
namespace app\api\model;
use think\Model;

class Invoices extends Model{   
    //多表关联    
    public function operator(){
        return $this->hasOne('manager','id','operator_id');
    }
    public function invoiceList($filter = null){
        $where = [];
        $page = 1;
        $pageCount = 10;
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }
            //模糊查询           
            if(!empty($filter['keyword'])){
                $where['company_name|mechanism_code|invoice_content'] = ['like',"%{$filter['keyword']}%"];               
            }    
            if(!empty($filter['invoice_no'])){
                $where['invoice_no'] = $filter['invoice_no'];               
            }  
            //模糊查询           
            if(!empty($filter['money'])){
                $where['invoice_price'] = $filter['money'];               
            }  
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;     
            //按操作员查询
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];
            }
            //按操作员查询
            if(!empty($filter['is_receive']) && $filter['is_receive']=="true"){
                $where['receipt_date'] = ['<>',''];
            }
        }
        $datas = $this->with("operator")->where($where)->page("$page,$pageCount")->order('id', 'desc')->select(); 
        $dataCount = $this->where($where)->count(); 
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;
    }

    public function keywordSearch($filter = null){
        $where = [];      
        if(!empty($filter)){            
            //模糊查询           
            if(!empty($filter['keyword'])){
                $keyword=trim($filter['keyword']);
                $where['company_name'] = ['like',"%{$keyword}%"];     
            }  
        }
        $datas = $this->where($where)->order('id', 'desc')->select(); 
        
        return $datas;
    }
}